<?php
!defined('P_W') && exit('Forbidden');
require_once (R_P . 'lib/base/systemdata.php');

/**
 * 出售房数据调用
 * @author bingbing.renbb
 *
 */
class PW_SaleSource extends SystemData {
	var $_lang = array(
			'houseid' => '房源ID',
			'title' => '标题',
			'room' => '户型',
			'acreage' => '面积',
			'price' => '总价',
			'village'=> '小区名称'
		);
	
	/**
	 * 获取提交的数据
	 * @param Array $config
	 * @param int $num
	 * @return array $_tmp
	 */
	function getSourceData($config, $num) {
		$config = $this->_initConfig($config);
		$_tmp = $this->_getData($config,$num);
		$_tmp = $_tmp ? $_tmp : array();
		foreach ($_tmp as $key => $value) {
			$_tmp[$key] = $this->_cookData($value);
		}
		return $_tmp;
	}
	
	/**
	 * 选择项
	 * @return array
	 */
	function getSourceConfig() {
		return array(
			'tag' =>array(
				'name'=>'房源标签',
				'type'=>'select',
				'value'=> array(
					'all'=>'不限',
					'top' => '置顶',
					'urgent' => '加急'
				)
			),
			'sortorder' => array(
				'name' => '排序方式',
				'type' => 'select',
				'value' => $this->_getSortList()
			),
			'type' => array(
				'name' => '发布来源',
				'type' => 'select',
				'value' => array(
					'all' => '不限',
					'broker' => '经纪人发布',
					'personal' => '个人发布'
				)
			)
		);
	}
	
	/**
	 * 处理传入的数据
	 * @param array $config
	 * @return array $temp
	 */
	function _initConfig($config) {
		$temp = array();
		list($temp['sortfield'], $temp['sorttype']) = explode(',', $config['sortorder']);
		($config['tag'] == 'all') || ($temp['expand'] = $config['tag'] == 'top' ? array('istop' => 1) : array('isurgent' => 1));
		($config['type'] == 'all') || ($temp['expand']['isbroker'] = $config['type'] == 'personal' ? 0 : 1);
		return $temp;
	}
	
	/**
	 * 查找符合条件的数据
	 * @param array $config
	 * @param int $num
	 * @return array
	 */
	function _getData($config,$num) {
		$saleSearch = $this->_getSecondSaleService();
		$tempResult = $saleSearch->getDetailList(array(), $config['sorttype'], $config['sortfield'], array(), 1, $num, $config['expand']); 
		$result = $tempResult[1];
		return $result;
	}
	
	/**
	 * 载入出租服务类
	 */
	function _getSecondSaleService(){
		return House::loadService('SecondSaleService');
	}
	
	/**
	 * 排序信息
	 * @return array
	 */
	function _getSortList() {
		return array(
			'updatetime,DESC'=>'按更新时间',
			'posttime,DESC'=>'按发布时间',
			'price,ASC'=>'按总价从低到高',
			'price,DESC'=>'按总价从高到低'
		);
	}
	
	/**
	 * 处理户型
	 * @param array $data
	 * @return array
	 */
	function _cookData($data) {
		$length = ($GLOBALS['db_charset'] == 'utf-8') ? 9 : 6;
		$data['room'] = substrs($data['room'], $length , 'N');
		$data['title']= $data['subject'];
		$data['url'] = 'index.php?m=house&q=sale&a=houseinfo&houseid='.$data['houseid'];
		return $data;	
	}	
}
?>